package com.chain.BrotherExchange.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.AppCompatImageView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chain.BrotherExchange.C;
import com.chain.BrotherExchange.R;
import com.chain.BrotherExchange.data.Resource;
import com.chain.BrotherExchange.ui.adapter.SimpleFragmentPagerAdapter;
import com.chain.BrotherExchange.ui.core.AbsNetworkStateActivity;
import com.chain.BrotherExchange.ui.fragment.UserLegalTenderDealListFragment;
import com.chain.BrotherExchange.utils.ImageViews;
import com.chain.BrotherExchange.viewmodel.LegalTenderDealViewModel;
import com.chain.BrotherExchange.viewmodel.core.ViewModelProviders;
import com.example.common.widget.CircleImageView;

import java.util.ArrayList;
import java.util.Arrays;

import butterknife.Bind;

/**
 * 用户法币交易信息
 */
public class UserLegalTenderTradeInfoActivity extends AbsNetworkStateActivity {

    @Bind(R.id.arrow_back_image) AppCompatImageView arrowBackImage;
    @Bind(R.id.user_avatar_image) CircleImageView userAvatarImage;
    @Bind(R.id.user_name_text) TextView userNameTextView;
    @Bind(R.id.register_date_text) TextView registerDateTextView;
    @Bind(R.id.total_deal_order_text) TextView totalDealOrderTextView;
    @Bind(R.id.th_deal_order_text) TextView thDealOrderTextView;
    @Bind(R.id.completion_rate_text) TextView completionRateTextView;
    @Bind(R.id.appeal_text) TextView appealTextView;
    @Bind(R.id.winning_text) TextView winningTextView;
    @Bind(R.id.average_release_text) TextView mAverageReleaseTextView;
    @Bind(R.id.tab_layout) TabLayout mTabLayout;
    @Bind(R.id.user_deal_details_view_pager) ViewPager userDealDetailsViewPager;
    @Bind(R.id.app_bar_layout) AppBarLayout mAppBarLayout;
    @Bind(R.id.real_name_authentication_layout) LinearLayout realNameAuthenticationLayout;
    @Bind(R.id.level_2_certification_layout) LinearLayout level2CertificationLayout;
    @Bind(R.id.phone_authentication_layout) LinearLayout phoneAuthenticationLayout;
    @Bind(R.id.title_text) TextView mTitleTextView;

    private int mUid;
    private LegalTenderDealViewModel mViewModel;

    public static void launch(Context context, int uid) {
        context.startActivity(new Intent(context, UserLegalTenderTradeInfoActivity.class)
                .putExtra(C.EXTRA_UID, uid)
        );
    }

    @Override
    protected int getLayoutID() {
        return R.layout.activity_look_user_legal_info;
    }

    @Override
    protected void initActions() {
        arrowBackImage.setOnClickListener(v -> onBackPressed());

        mUid = getIntent().getIntExtra(C.EXTRA_UID, 0);
        mViewModel = ViewModelProviders.of(this).get(LegalTenderDealViewModel.class);

        initUserInfo();

        initUserDealList();

        // 关闭
        mAppBarLayout.addOnOffsetChangedListener((appBarLayout, i) -> {
            if (Math.abs(i) == appBarLayout.getTotalScrollRange()) {
                mTitleTextView.setText(userNameTextView.getText().toString() +
                        getString(R.string.transaction_information));
                String title = mTitleTextView.getText().toString();
                if (title.length() > 10) {
                    mTitleTextView.setText(title.substring(0, 2) + "..."
                            + title.substring(title.length() - 5, title.length()));
                }
            } else if (i == 0) {
                mTitleTextView.setText(null);
            }
        });
    }

    private void initUserDealList() {
        mTabLayout.setupWithViewPager(userDealDetailsViewPager);
        userDealDetailsViewPager.setAdapter(new SimpleFragmentPagerAdapter(
                getSupportFragmentManager(),
                new ArrayList<>(Arrays.asList(
                        UserLegalTenderDealListFragment.newInstance(1, mUid),
                        UserLegalTenderDealListFragment.newInstance(2, mUid)
                )),
                new ArrayList<>(
                        Arrays.asList(getString(R.string.buy), getString(R.string.sell))
                )));
    }

    private void initUserInfo() {
        mViewModel.legalTenderUserInfo(mUid).observe(this, resource -> {
            Resource.load(this, resource, this, result -> {
                ImageViews.load(result.getHead_scul(), userAvatarImage);
                userNameTextView.setText(result.getNick_name());
                registerDateTextView.setText(String.format(getString(R.string.placeholder_register_date),
                        result.getCreated_time()));

                completionRateTextView.setText(result.getComplete_rate() + "%"); // 完成率
                appealTextView.setText(result.getAppeal() + getString(R.string.text_times)); // 申诉
                winningTextView.setText(result.getSuccess() + getString(R.string.text_times)); // 胜诉
                totalDealOrderTextView.setText(result.getOrders() + getString(R.string.text_times)); // 总成单
                thDealOrderTextView.setText(result.getMonth_rate() + getString(R.string.text_times)); // 20成单
                mAverageReleaseTextView.setText(String.valueOf(result.getAverage_to())); // 平均放行

                if (result.getPhone_auth() == 1) {
                    phoneAuthenticationLayout.setVisibility(View.VISIBLE);
                }
                if (result.getReal_name() == 1) {
                    realNameAuthenticationLayout.setVisibility(View.VISIBLE);
                }
                if (result.getTwo_level_auth() == 1) {
                    level2CertificationLayout.setVisibility(View.VISIBLE);
                }
            });
        });
    }

    @Override
    public void onAnewRequest() {
        initUserInfo();
    }

}
